<?php
/**
 * Created by PhpStorm.
 * User: wincol
 * Date: 2018/3/24
 * Time: 20:07
 */

namespace App\Task;


use App\Models\Novel;
use App\Models\ReadHistory;
use App\Models\User;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Support\Facades\Log;

class RecordByContentTask extends Task
{
    public $novel_id;
    public $chapter_id;
    public $content_size;
    public $user_id;
    public function __construct($novel_id, $chapter_id, $content_size, $user_id='')
    {
        $this->novel_id     = $novel_id;
        $this->chapter_id   = $chapter_id;
        $this->content_size = $content_size;
        $this->user_id      = $user_id;
    }
    public function handle()
    {
        //有用户才记录
        if($this->user_id <> ''){
            $history = ReadHistory::where([
                'user_id'=>$this->user_id,
                'chapter_id'=>$this->chapter_id])->first();
            //该篇没看过
            if(empty($history)){
                ReadHistory::create([
                    'user_id'=>$this->user_id,
                    'chapter_id'=>$this->chapter_id,
                    'content_size'=>$this->content_size,
                    'novel_id'=>$this->novel_id
                ]);
                $history = ReadHistory::where([
                    'user_id'=>$this->user_id,
                    'novel_id'=>$this->novel_id])->first();
                //该章没看过
                if(empty($history)){
                    Novel::where('id',$this->novel_id)->increment('reading_num');
                    User::where('id',$this->user_id)->increment('reading_num');
                }
            }
        }
    }
}